let jwt = require("jsonwebtoken")
const filter = (req, res, next) => {
    let token = req.body.token || req.query.token || req.headers['authorization']
    //只要不是登入注册 通通拦截
    if (req.path == "/users/login" || req.path == "/users/regist") {
        //如果是登入或者注册  放行
        next()
    } else {
        //验证token
        let sercrect = "MY_NAME_IS_LILEI"
        jwt.verify(token, sercrect, (err, decode) => {
            if (err) {
                res.send({ code: 304, msg: "验证失败", statu: false })
            } else {
                //判断 如果不是check接口 就直接放行
                if (req.path !== "/check") {
                    next()
                } else {
                    res.send({ code: 200, msg: "验证成功", status: true, data: decode })
                }

            }
        })
    }

    console.log(req.path)
}
module.exports = {
    filter
} 